import { createApp, asyncDataFilter } from "./main"
//TODO: 引入数据库和对象仓库
import airbnb from './db'

const { app, router, store } = createApp()

if ((window as any).__INITIAL__STATE__) {
    store.replaceState((window as any).__INITIAL__STATE__)
}

router.beforeEach(async (to, from, next) => {
    await airbnb.airbnbDB.openStore({
        ...airbnb.languageObjectStore,
        ...airbnb.userObjectStore,
        ...airbnb.orderObjectStore,
        ...airbnb.recordObjectStore
    }).then((res: any) => {
        console.log("初始化所有对象仓库", res);
        localStorage.getItem("userId") && store.commit('setUserStatus', 1)
    })
    next()
})

router.isReady().then(() => {
    router.beforeResolve((to, from, next) => {
        let toComponents = router.resolve(to).matched.flatMap((record: any) =>
            Object.values(record.components)
        )
        let fromComponents = router.resolve(from).matched.flatMap((record: any) =>
            Object.values(record.components)
        )

        let actived = toComponents.filter((c, i) => {
            return fromComponents[i] !== c
        })
        if (!actived.length) {
            return next()
        }
        console.log("开始loading.....");

        asyncDataFilter(actived, store, router.currentRoute).then(() => {
            console.log("结束loading.....");
            console.log(store.state);

            next()
        })
    })

    app.mount("#app")
})


router.afterEach((to, from, next) => {
    const { roomDetail } = store.state
    const { title: roomTitle = "", owner } = roomDetail || {}
    const { introduce = "" } = owner || {}
    const { meta } = to
    const { title, keywords, description } = meta
    if (title) {
        document.title = `${title}${roomTitle}`
    } else {
        document.title = ""
    }
    const keywordsMeta = document.querySelector('meta[name="keywords"]')
    keywordsMeta && keywordsMeta.setAttribute("content", `${keywords}${introduce}`)

    const descriptionMeta = document.querySelector('meta[name="description"]')
    descriptionMeta?.setAttribute("content", `${description}${introduce}`)

})